package com.dhec.ptms.epr.db;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhec.ptms.epr.model.TestStandard;
import com.ionglobal.db.DBI;
import com.ionglobal.lib.SysException;

public class TestStandardDB {

	private static final Logger logger = LoggerFactory.getLogger(TestStandardDB.class);
	
	public boolean clearByRecordId(DBI dbi, int recordid) {
		boolean rs = false;
		try {
			String sql = "delete from EPR_TestStandard where recordid="+recordid;
			rs = dbi.execute(sql);
		} catch (SysException e) {
			logger.error("TestStandardDB.clearByRecordId(DBI, int) Error!", e);
		}

		return rs;
	}
	
	public void handleData(DBI dbi, TestStandard bean) throws SysException{
		bean.setId(dbi.rsGetLong("id"));
		bean.setItem(dbi.rsGetString("item"));
		bean.setRecordid(dbi.rsGetInt("recordid"));
		bean.setReportid(dbi.rsGetInt("reportid"));
		bean.setResult(dbi.rsGetString("result"));
		bean.setStandard(dbi.rsGetString("standard"));
		bean.setTestdate(dbi.rsGetDate("testdate"));
	}
	
	public List<TestStandard> list(DBI dbi, int recordid) throws SysException{
		List<TestStandard> l = new ArrayList<TestStandard>();
		dbi.execQuery("select * from EPR_TestStandard where recordid="+recordid);
		while(dbi.rsNext()){
			TestStandard bean = new TestStandard();
			handleData(dbi, bean);
			l.add(bean);
		}
		dbi.rsClose();
		
		return l;
	}
	
	public boolean insert(DBI dbi, TestStandard bean) {
		try {
			return dbi
					.execute("INSERT INTO EPR_TestStandard VALUES ("+bean.getId()+", "+bean.getReportid()
							+", "+bean.getRecordid()+", '"+dbi.fixSql(bean.getItem())+"', '"
							+dbi.fixSql(bean.getStandard())+"', '"+dbi.fixSql(bean.getResult())+"', null)");
		} catch (SysException e) {
			logger.error("TestStandardDB.insert(DBI, TestStandard) Error!", e);
			return false;
		}
	}
	
	public boolean update(DBI dbi, TestStandard bean) {
		try {
			return dbi
					.execute("UPDATE EPR_TestStandard SET result = '"+dbi.fixSql(bean.getResult())+"' WHERE id = "+bean.getId());
		} catch (SysException e) {
			logger.error("TestStandardDB.update(DBI, TestStandard) Error!", e);
			return false;
		}
	}
}
